package link;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class Sort {

//    public static void main(String[] args) {
//        int[] arr = {3,3,2,5,6,3,8,5,3,1,9,4,3};
//    }

    // 归并排序
    public static void mergeSort(int[] arr,int start,int end) {
        if (start < end){
            int mid = (start + end) / 2;
            mergeSort(arr, start, mid);
            mergeSort(arr, mid + 1, end);
            merge(arr, start, mid, end);
        }

    }

    public static void merge(int[] arr,int start,int mid,int end){
        int[] tmpArr = new int[end - start + 1];
        int p1 = start;
        int p2 = mid + 1;
        int p = 0;
        while (p1 <= mid && p2 <= end){
            if(arr[p1] <= arr[p2]){
                tmpArr[p++] = arr[p1++];
            }else{
                tmpArr[p++] = arr[p2++];
            }
        }

        while (p1 <= mid){
            tmpArr[p++] = arr[p1++];
        }
        while (p2 <= end){
            tmpArr[p++] = arr[p2++];
        }
        for(int i = 0; i < tmpArr.length;i++){
            arr[start + i] = tmpArr[i];
        }

    }



    // 快速排序
    public static void quickSort(int[] arr, int startIndex, int endIndex) {
// 递归结束条件：startIndex大等于endIndex的时候
        if (startIndex >= endIndex) {
            return;
        }
// 得到基准元素位置
        int pivotIndex = partition(arr, startIndex, endIndex);
// 用分治法递归数列的两部分
        quickSort(arr, startIndex, pivotIndex - 1);
        quickSort(arr, pivotIndex + 1, endIndex);
        System.out.println(1);
    }

    public static int partition(int[] nums,int l,int r){
        int base = nums[l];
        int left = l;
        int right = r;

        while (left < right){
            while (left < right && nums[right] > base){
                right--;
            }
            while(left < right && nums[left] <= base){
                left++;
            }
            if(left < right){
                int tmp = nums[left];
                nums[left] = nums[right];
                nums[right] = tmp;
            }
        }
        nums[l] = nums[left];
        nums[left] = base;
        return left;
    }
    public static void main(String[] args) {

        int[] arr = new int[] {25,14,9,1};

        quickSort(arr, 0, arr.length-1);
//        mergeSort(arr,0,arr.length - 1);
        System.out.println(Arrays.toString(arr));

    }

}
